package gw.com.android.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.JsonUtil;


/**
 * Created by reeta.zou on 2016/9/23.
 */
public class AppJsonUtil {

    /**
     * 返回资讯列表的数据
     * @param jsonStr
     * @return
     */
    public static DataItemResult getNewsList(String jsonStr) {
        DataItemResult itemResult = new DataItemResult();
        try {
            if(!JsonUtil.isJsonData(jsonStr)){
                return itemResult;
            }
            JSONObject object = new JSONObject(jsonStr);
            for (Iterator<String> keys = object.keys(); keys.hasNext();) {
                String key = keys.next();
                //判断key值是数字
                if(toIntData(key) != -1){
                    JSONObject valueObject = (JSONObject) object.opt(key);
                    if(null != valueObject){
                        DataItemDetail item = new DataItemDetail();
                        JsonUtil.toDataItemDetail(item, valueObject);
                        itemResult.addItem(item);
                    }
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return itemResult;
    }

    /**
     * 将字符串转换成int类型数字
     * @param data
     * @return
     */
    public static int toIntData(String data){
        if(null != data && data.length() > 0){
            boolean isNum = data.matches("[0-9]+");
            if(isNum){
                return Integer.valueOf(data);
            }
        }
        return -1;
    }

    /**
     * 返回消息中心列表数据
     * @param jsonStr
     * @return
     */
    public static DataItemResult getMessageList(String jsonStr){
        DataItemResult itemResult = new DataItemResult();
        try {
            if(!JsonUtil.isJsonData(jsonStr)){
                return itemResult;
            }
            JSONObject object = new JSONObject(jsonStr);
            JsonUtil.toDataItemResult(itemResult,object.get("data"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return itemResult;
    }

    /**
     * 返回消息中心列表数据  openApi
     * @param jsonStr
     * @return
     */
    public static DataItemResult getMessageList2(String jsonStr){
        DataItemResult itemResult = new DataItemResult();
        try {
            if(!JsonUtil.isJsonData(jsonStr)){
                return itemResult;
            }
            JSONObject object = new JSONObject(jsonStr);
            JsonUtil.toDataItemResult(itemResult,object.getJSONObject("data").get("list"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return itemResult;
    }


    /**
     * 处理广告接口数据
     * @return
     */
    public static JSONObject dealAds(String jsonStr){
        if(!JsonUtil.isJsonData(jsonStr)){
            return null;
        }
        JSONObject object = null;
        try {
            object = new JSONObject(jsonStr);
            if(!object.has("data")){
                return null;
            }
            object = object.optJSONObject("data");
            String type = "";
            if(GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_REAL){
                type = AppContances.ADS_USER_TYPE_REAL;
            }else if(GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_DEMO){
                type = AppContances.ADS_USER_TYPE_DEMO;
            }else {
                type = AppContances.ADS_USER_TYPE_GUEST;
            }
            object = object.optJSONObject(type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return object;
    }

    /**
     * 返回指定类型的广告的数据
     * @param jsonStr
     * @return
     */
    public static DataItemResult getConfigAdsList(String jsonStr, int displayPosition) {
        DataItemResult itemResult = new DataItemResult();
//        Logger.e("弹层广告处理 getConfigAdsList====  " + jsonStr);
        JSONObject object = dealAds(jsonStr);
        if(object == null){
            return itemResult;
        }
//        Logger.e("弹层广告处理 getConfigAdsList2====  " + object.toString());
        for (Iterator<String> keys = object.keys(); keys.hasNext();) {
            String key = keys.next();
//            Logger.e("弹层广告处理 key====  " + key);
            //判断key值是数字
            if(toIntData(key) != -1 && toIntData(key) == displayPosition){
                JSONObject valueObject = (JSONObject) object.opt(key);
                if(null != valueObject){
                    JSONArray array = valueObject.optJSONArray("adPics");
                    if(null != array && array.length() > 0){
                        JsonUtil.toDataItemResult(itemResult, array);
                    }
                }
            }
        }
//        Logger.e("弹层广告处理 getConfigAdsList3====  ");
        return itemResult;
    }

}
